/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OHOS_SLE_LOG_H
#define OHOS_SLE_LOG_H

#ifdef __LITEOS_M__
#include "sle_hilog_lite.h"
#else

#ifndef SLE_HILOG_TAG
#define SLE_HILOG_TAG "SleService"
#endif

#ifndef SLE_LOG_DOMAIN
#define SLE_LOG_DOMAIN 0xD00D108
#endif

#include "hilog/log.h"

#ifdef __cplusplus
static constexpr OHOS::HiviewDFX::HiLogLabel SLE_LABEL = {LOG_CORE, SLE_LOG_DOMAIN, SLE_HILOG_TAG};


#define R_FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)

#define __SLE_LOG(func, fmt, args...)                                                                               \
    do {                                                                                                            \
        (void)func(SLE_LABEL, "[SleService][%{public}s:%{public}d][%{public}s()] " fmt, R_FILENAME__, __LINE__,  \
                   __FUNCTION__, ##args);                                                                           \
    } while (0)


#define SLE_LOGD(fmt, ...) __SLE_LOG(OHOS::HiviewDFX::HiLog::Debug, fmt, ##__VA_ARGS__)
#define SLE_LOGI(fmt, ...) __SLE_LOG(OHOS::HiviewDFX::HiLog::Info, fmt, ##__VA_ARGS__)
#define SLE_LOGW(fmt, ...) __SLE_LOG(OHOS::HiviewDFX::HiLog::Warn, fmt, ##__VA_ARGS__)
#define SLE_LOGE(fmt, ...) __SLE_LOG(OHOS::HiviewDFX::HiLog::Error, fmt, ##__VA_ARGS__)
#define SLE_LOGF(fmt, ...) __SLE_LOG(OHOS::HiviewDFX::HiLog::Fatal, fmt, ##__VA_ARGS__)

#else // #__cplusplus

#undef SLE_HILOG_TAG
#define SLE_HILOG_TAG "SleHal"

#define SLE_LOGD(...) ((void)HiLogPrint(LOG_CORE, LOG_DEBUG, SLE_LOG_DOMAIN, SLE_HILOG_TAG, __VA_ARGS__))

#define SLE_LOGI(...) ((void)HiLogPrint(LOG_CORE, LOG_INFO, SLE_LOG_DOMAIN, SLE_HILOG_TAG, __VA_ARGS__))

#define SLE_LOGW(...) ((void)HiLogPrint(LOG_CORE, LOG_WARN, SLE_LOG_DOMAIN, SLE_HILOG_TAG, __VA_ARGS__))

#define SLE_LOGE(...) ((void)HiLogPrint(LOG_CORE, LOG_ERROR, SLE_LOG_DOMAIN, SLE_HILOG_TAG, __VA_ARGS__))

#define SLE_LOGF(...) ((void)HiLogPrint(LOG_CORE, LOG_FATAL, SLE_LOG_DOMAIN, SLE_HILOG_TAG, __VA_ARGS__))

#endif // #__cplusplus

/**
 * @brief assert condition and return if failed.
 *
 * @param cond condition to assert
 * @return None
 */
#define SLE_ASSERT_AND_RETURN(cond)                              \
    do {                                                        \
        if (!(cond)) {                                          \
            SLE_LOGE("%s, assert failed!", #cond);               \
            return;                                             \
        }                                                       \
    } while (0)

/**
 * @brief assert condition and return ret if failed.
 *
 * @param cond condition to assert
 * @param ret return value
 * @return ret
 */
#define SLE_ASSERT_AND_RETURN_RET(cond, ret)                                             \
    do {                                                                                \
        if (!(cond)) {                                                                  \
            if (ret == 0) {                                                             \
                SLE_LOGW("%s, assert failed! ret: %s.", #cond, #ret);                    \
            } else {                                                                    \
                SLE_LOGE("%s, assert failed! ret: %s.", #cond, #ret);                    \
            }                                                                           \
            return ret;                                                                 \
        }                                                                               \
    } while (0)

/**
 * @brief assert condition and print log if failed.
 *
 * @param cond condition to assert
 * @param fmt log format
 * @return return
 */
#define SLE_ASSERT_AND_RETURN_LOG(cond, fmt, ...)  \
    do {                                      \
        if (!(cond)) {                        \
            SLE_LOGE(fmt, ##__VA_ARGS__); \
            return;                           \
        }                                     \
    } while (0)

/**
 * @brief assert condition and print log if failed.
 *
 * @param cond condition to assert
 * @param fmt log format
 * @return do not return
 */
#define SLE_ASSERT_AND_PRINT_LOG(cond, fmt, ...)  \
    do {                                      \
        if (!(cond)) {                        \
            SLE_LOGE(fmt, ##__VA_ARGS__); \
        }                                     \
    } while (0)

/**
 * @brief assert condition and return ret with log if failed.
 *
 * @param cond condition to assert
 * @param ret return value
 * @param fmt log format
 * @return ret
 */
#define SLE_ASSERT_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
    do {                                              \
        if (!(cond)) {                                \
            if ( ret == 0) {                          \
                SLE_LOGW(fmt, ##__VA_ARGS__);         \
            } else {                                  \
                SLE_LOGE(fmt, ##__VA_ARGS__);         \
            }                                         \
            return ret;                               \
        }                                             \
    } while (0)



#endif //__LITEOS_M__
#endif //OHOS_SLE_LOG_H